package com.yxy.service.impl;

import com.yxy.mapper.HotelBookingMapper;
import com.yxy.mapper.RouteMapper;
import com.yxy.mapper.SightsBookingMapper;
import com.yxy.mapper.SightsMapper;
import com.yxy.pojo.vo.SightsVo;
import com.yxy.pojo.vo.StatisticsVO;
import com.yxy.service.CommonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CommonServiceImpl implements CommonService {

    @Autowired
    private SightsMapper sightsMapper;
    @Autowired
    private RouteMapper routeMapper;
    @Autowired
    private SightsBookingMapper sightsBookingMapper;
    @Autowired
    private HotelBookingMapper hotelBookingMapper;

    /**
     * 统计数据
     * @return
     */
    @Override
    public StatisticsVO statistics() {
        StatisticsVO statisticsVO = new StatisticsVO();
        // 获取景点浏览量
        List<Integer> sightsViews = sightsMapper.getViews();
        // 获取线路浏览量
        List<Integer> routeViews = routeMapper.getViews();
        // 获取景点预约量
        List<Integer> sightsBooking = sightsBookingMapper.getAll();
        // 获取酒店预约量
        List<Integer> hotelBooking = hotelBookingMapper.getAll();

        sightsViews.replaceAll(item -> item == null ? 0 : item);
        routeViews.replaceAll(item -> item == null ? 0 : item);

        statisticsVO.setSightsViews(sightsViews.stream().mapToInt(Integer::intValue).sum());
        statisticsVO.setRouteViews(routeViews.stream().mapToInt(Integer::intValue).sum());
        statisticsVO.setSightsBooking(sightsBooking.size());
        statisticsVO.setHotelBooking(hotelBooking.size());
        return statisticsVO;
    }
}
